package gxprod.androidai.functions;

import gxprod.androidai.AIActivity;
import gxprod.androidai.BubbleAdapter;
import gxprod.androidai.regex.MatchResult;
import gxprod.androidai.regex.NamedPattern;
import gxprod.androidai.support.EquationSolver;
import android.util.Log;


/**
 * Interactions between the user and the ai, such as saying hello
 * @author Josh Maleszewski
 * @author Austin Bolstridge
 */
public class MathFunction extends Function {

	private NamedPattern[] NamedPatterns = new NamedPattern[] {
			NamedPattern.compile("solve ([0-9\\+\\-a-z\\(\\)\\*\\\\%\\^/ ]*)"),
	};

	@Override
	public boolean cullFunction(String statement) { return statement.contains("solve"); }
	
	@Override
	public boolean execute(MatchResult result, BubbleAdapter adapter) {
		switch(result.MatchIndex) {

			case 0:
			case 1:
				String equasion = result.Matcher.group(1);
				equasion = compileMathStatement(equasion);

				String solved = EquationSolver.solve(equasion);
				adapter.output(solved);

				return true;
			default :
				Log.e(AIActivity.TAG, "invalid match result index: " + result.MatchIndex + " in InteractionFunction");
				return false;
		}
	}

	private String compileMathStatement(String in) {

		in = in.replace(" cosine ", " cos( ");
		in = in.replace(" sine ", " sin( ");
		in = in.replace(" tangent ", " cos( ");
		in = in.replace(" arcsine ", " acos( ");
		in = in.replace(" arcsine ", " acos( ");
		in = in.replace(" arcsine ", " acos( ");
		in = in.replace(" square root", "^(.5) ");
		in = in.replace(" cube root", "^(1/3) ");
		in = in.replace(" squared ", "^2 ");
		in = in.replace(" cubed ", "^3 ");
		return in;
	}

	@Override
	public MatchResult isMatch(String statement) { return super.isMatch(statement, NamedPatterns, 0.65f); }
}
